//
//  ZRTabBar.swift
//  Sina
//
//  Created by 张锐 on 16/2/24.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit

class ZRTabBar: UITabBar {
    
    //重写init方法
    override init(frame: CGRect) {
        super.init(frame:frame)
        
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //视图添加控件和约束的方法
    func setUpUI(){
    
    addSubview(centerButton)
    
    }
    
    //懒加载控件
    
    lazy var centerButton:UIButton = {
    
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        
        
        return button
        
    
    }()
    
    
    
    
    //调整子控件的位置
    override func layoutSubviews() {
        
        super.layoutSubviews()
        
        var index = 0
        let width = UIScreen.mainScreen().bounds.width / 5
        let height = bounds.height
        
        for subview in subviews {
  
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!){
                if index == 2{
                    centerButton.frame=CGRectMake(CGFloat(index) * width, 0, width, height)
                    index++}
                subview.frame = CGRectMake(CGFloat(index++) * width, 0, width, height)
            
            }
        
        }
        
        
    }

}
